package com.mmlc.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.hardware.Camera;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;

public class PhoneUtils {

    private static final String TAG = "tag";

    /**
     * 手机型号
     */
    public static String getModel() {//Android SDK built for x86_64
        Log.e(TAG, "getModel:" + Build.MODEL);
        return Build.MODEL;
    }

    /**
     * 手机版本号
     */
    public static String getPhoneVersion() {
        Log.e(TAG, "getPhoneVersion:" + Build.VERSION.SDK);
        return Build.VERSION.SDK;
    }

    /**
     * android版本
     */
    public static String getAndroidVersion() {
        Log.e(TAG, "getAndroidVersion:" + Build.VERSION.RELEASE);
        return Build.VERSION.RELEASE;
    }

    /**
     * CPU
     */
    public static String getCPU(Context context) {
//        Log.e(TAG, "getCPU:" + getCpuInfo().toString());
        return getCpuInfo()[0];
    }

    // 3.手机CPU信息
    // 和内存信息同理，cpu信息可通过读取/proc/cpuinfo文件来得到，其中第一行为cpu型号，第二行为cpu频率。
    public static String[] getCpuInfo() {
        String str1 = "/proc/cpuinfo";
        String str2 = "";
        String[] cpuInfo = {"", ""}; // 1-cpu型号 //2-cpu频率
        String[] arrayOfString;
        try {
            FileReader fr = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
            str2 = localBufferedReader.readLine();
            arrayOfString = str2.split("\\s+");
            for (int i = 2; i < arrayOfString.length; i++) {
                cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
            }
            str2 = localBufferedReader.readLine();
            arrayOfString = str2.split("\\s+");
            cpuInfo[1] += arrayOfString[2];
            localBufferedReader.close();
        } catch (IOException e) {
        }
        Log.e(TAG, "cpuinfo:" + cpuInfo[0] + " " + cpuInfo[1]);
        return cpuInfo;
    }

    /**
     * 基带
     */
    public static String getBB() {
        try {
            Class cl = Class.forName("android.os.SystemProperties");
            Object invoker = cl.newInstance();
            Method m = cl.getMethod("get", new Class[]{String.class, String.class});
            Object result = m.invoke(invoker, new Object[]{"gsm.version.baseband", "no message"});
            System.out.println("基带版本: " + (String) result);

            Log.e(TAG, "getBB:" + (String) result);
            return (String) result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 手机号码运营商  0=未知 , 1=电信 , 2=移动 , 3=联通
     */
    public static int getProvidersName(Context context) {
        String IMSI = getIMSI(context);
        // IMSI号前面3位460是国家，紧接着后面2位00 02是中国移动，01是中国联通，03是中国电信。
        Log.e(TAG, "getProvidersName:" + IMSI);
        if(TextUtils.isEmpty(IMSI)){
            return 0;
        }
        if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
            return 2;
        } else if (IMSI.startsWith("46001")) {
            return 3;
        } else if (IMSI.startsWith("46003")) {
            return 1;
        }
        return 0;
    }

    /**
     * IMSI
     */
    public static String getIMSI(Context context) {
        TelephonyManager mTm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
        return mTm.getSubscriberId();
    }

    /**
     * IMEI
     */
    public static String getIMEI(Context context) {
        TelephonyManager mTm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);

        Log.e(TAG, "getIMEI:" + mTm.getDeviceId());

        return mTm.getDeviceId();
    }

    /**
     * MAC
     */
    public static String getMac(Context context) {
        String result = "";
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        result = wifiInfo.getMacAddress();

        Log.e(TAG, "getMac:" + result);

        return result;
    }

    /**
     * 内核版本
     */
    public static String getKernelVersion() {
        String kernelVersion = "";
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("/proc/version");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return kernelVersion;
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 8 * 1024);
        String info = "";
        String line = "";
        try {
            while ((line = bufferedReader.readLine()) != null) {
                info += line;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            if (info != "") {
                final String keyword = "version ";
                int index = info.indexOf(keyword);
                line = info.substring(index + keyword.length());
                index = line.indexOf(" ");
                kernelVersion = line.substring(0, index);
            }
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        }

        Log.e(TAG, "getKernelVersion:" + kernelVersion);

        return kernelVersion;
    }

    /**
     * 前置摄像头支持
     */
    public static int getForeCameraSupport(Context context) {
        int numberOfCameras = Camera.getNumberOfCameras();
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                return 1;
            }
        }
        return 0;
    }

    /**
     * 后置摄像头支持
     */
    public static int getBackCameraSupport(Context context) {
        int numberOfCameras = Camera.getNumberOfCameras();
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                return 1;
            }
        }
        return 0;
    }

    /**
     * 获取android当前可用内存大小
     */
    public static String getAvailMemory(Context context) {

        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(mi);

//        return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化
        return formatSize(mi.availMem);
    }

    /**
     * 获取android总内存大小
     */
    public static String getTotalMemory(Context context) {
        String str1 = "/proc/meminfo";// 系统内存信息文件
        String str2;
        String[] arrayOfString;
        long initial_memory = 0;

        try {
            FileReader localFileReader = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
            str2 = localBufferedReader.readLine();// 读取meminfo第一行，系统总内存大小

            arrayOfString = str2.split("\\s+");
            for (String num : arrayOfString) {
                Log.e(str2, num + "\t");
            }
            /**  获得系统总内存，单位是KB，乘以1024转换为Byte */
            initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;
            localBufferedReader.close();

        } catch (IOException e) {
        }
        /**  Byte转换为KB或者MB，内存大小规格化 */
//        return Formatter.formatFileSize(context, initial_memory);
        return formatSize(initial_memory);
    }

    public static String formatSize(long number) {
        return String.valueOf(number / 1024 / 1024);
    }
}